package com.yellowpage.nsp.action;
  
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;




import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;
import com.yellowpage.nsp.data.NspLogger;
import com.yellowpage.nsp.service.NspLoggerService;

public class NspLoggerAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	public String RESULT_NSPCOUNT = "nsp_count";
	public String RESULT_NSPCOUNT_INFO = "nsp_count_info";
	
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<NspLogger> nspLoggerList = new ArrayList<NspLogger>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private NspLogger nspLogger;
	private NspLoggerService nspLoggerService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_logId;
		private String query_requestTime;
		private String query_requestTime_end;
		private String query_requestInfo;
		private String query_requestType;
		private String query_requestIp;
		private String query_responseData;
		private String query_responseInfo;
		private String query_id;
		private String query_responseCode;
		private String query_requestType_seach;
		
		
		private Map requestTypeMap = new HashMap();
		private Map responseCodeMap = new HashMap();
		
		
	public void prepare() {
		super.prepare();
		responseCodeMap=nspLoggerService.getresponseCodeMap();
		requestTypeMap=nspLoggerService.getrequestTypeMap();
		/*requestTypeMap.put(0, "查电话");
		requestTypeMap.put(1, "查关键字");
		requestTypeMap.put(2, "查详情");
		requestTypeMap.put(3, "分类查询");
		requestTypeMap.put(4, "标记");
		requestTypeMap.put(5, "对接360");
		requestTypeMap.put(6, "品牌");
		requestTypeMap.put(7, "对接21CN");
		requestTypeMap.put(8, "对接号簿助手");
		requestTypeMap.put(9, "通信助理");
		requestTypeMap.put(10, "测试");
		requestTypeMap.put(11, "手机安全");
		requestTypeMap.put(12, "手机安全标记");
		requestTypeMap.put(13, "通用测试");
		requestTypeMap.put(14, "电话查详情");
		requestTypeMap.put(15, "锁屏查电话");
		requestTypeMap.put(16, "锁屏查关键字");
		requestTypeMap.put(17, "锁屏查电话");
		requestTypeMap.put(18, "锁屏查关键字");
		requestTypeMap.put(19, "手机安全正查");
		requestTypeMap.put(20, "查询手机安全标记");
		requestTypeMap.put(21, "哦啦查关键字");*/
		
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspLogger = nspLoggerService.getNspLogger(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("logId", query_logId);
	    	    queryMap.put("requestTime", query_requestTime);
	    	    queryMap.put("requestInfo", query_requestInfo);
	    	    queryMap.put("requestType", query_requestType);
	    	    queryMap.put("requestIp", query_requestIp);
	    	    queryMap.put("responseData", query_responseData);
	    	    queryMap.put("responseInfo", query_responseInfo);
	    	    queryMap.put("id", query_id);
	    	    queryMap.put("responseCode", query_responseCode);
	    	    	
		resultList = nspLoggerService.queryNspLoggerByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String gotoCountNspLogger() {
		return RESULT_NSPCOUNT;
	}
	
	public String countNspLogger() {
		
		Map queryMap = new HashMap();	
		queryMap.put("logId", query_logId);
		queryMap.put("requestTime", query_requestTime);
		queryMap.put("requestTimeEnd", query_requestTime_end);
		queryMap.put("requestInfo", query_requestInfo);
		queryMap.put("requestType", query_requestType);
		queryMap.put("requestIp", query_requestIp);
		queryMap.put("responseData", query_responseData);
		queryMap.put("responseInfo", query_responseInfo);
		queryMap.put("id", query_id);
		queryMap.put("responseCode", query_responseCode);
		
		
	    	    	
		resultList = nspLoggerService.countNspLogger(queryMap);
		
		//setPageRenderToRequest();
		
		return RESULT_NSPCOUNT;
	}
	
	public String getSearchInfo() {
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		if(query_requestTime == null){
			query_requestTime = sFormat.format(new Date());
		}
		if(query_requestTime_end == null){
			query_requestTime_end = sFormat.format(new Date());
		}
		
		Map queryMap = new HashMap();	
		queryMap.put("logId", query_logId);
		queryMap.put("requestTime", query_requestTime);
		queryMap.put("requestTimeEnd", query_requestTime_end);
		queryMap.put("requestInfo", query_requestInfo);
		queryMap.put("requestType", query_requestType_seach);
		queryMap.put("requestIp", query_requestIp);
		queryMap.put("responseData", query_responseData);
		queryMap.put("responseInfo", query_responseInfo);
		queryMap.put("id", query_id);
		queryMap.put("responseCode", query_responseCode);
		
		//Map map = ActionContext.getContext().getParameters();
		
		//String requestType = map.get("requestType").toString();
		
		if(!query_requestType_seach.isEmpty()){
			resultList = nspLoggerService.getseachInfo(queryMap);
		}
		
		return RESULT_NSPCOUNT_INFO;
		
	}
	
	
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		nspLoggerService.saveNspLogger(nspLogger);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
	    	    	    if (nspLogger.getLogId().length() == 0) {
			this.addActionError("** LogId 不能为空 !");
		}
			    	    	    if (nspLogger.getRequestTime().length() == 0) {
			this.addActionError("** RequestTime 不能为空 !");
		}
			    	    	    if (nspLogger.getRequestInfo().length() == 0) {
			this.addActionError("** RequestInfo 不能为空 !");
		}
			    	    	    if (nspLogger.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}
			    	    	    if (nspLogger.getRequestIp().length() == 0) {
			this.addActionError("** RequestIp 不能为空 !");
		}
			    	    	    if (nspLogger.getResponseData().length() == 0) {
			this.addActionError("** ResponseData 不能为空 !");
		}
			    	    	    if (nspLogger.getResponseInfo().length() == 0) {
			this.addActionError("** ResponseInfo 不能为空 !");
		}
			    	    	    	    	    if (nspLogger.getResponseCode().length() == 0) {
			this.addActionError("** ResponseCode 不能为空 !");
		}
			    	    
		this.setValidatorResult("/nsp/NspLoggerAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    nspLogger = nspLoggerService.getNspLogger(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    nspLoggerService.updateNspLogger(nspLogger);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    if (nspLogger.getLogId().length() == 0) {
			this.addActionError("** LogId 不能为空 !");
		}
			    			    if (nspLogger.getRequestTime().length() == 0) {
			this.addActionError("** RequestTime 不能为空 !");
		}
			    			    if (nspLogger.getRequestInfo().length() == 0) {
			this.addActionError("** RequestInfo 不能为空 !");
		}
			    			    if (nspLogger.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}
			    			    if (nspLogger.getRequestIp().length() == 0) {
			this.addActionError("** RequestIp 不能为空 !");
		}
			    			    if (nspLogger.getResponseData().length() == 0) {
			this.addActionError("** ResponseData 不能为空 !");
		}
			    			    if (nspLogger.getResponseInfo().length() == 0) {
			this.addActionError("** ResponseInfo 不能为空 !");
		}
			    			    			    if (nspLogger.getResponseCode().length() == 0) {
			this.addActionError("** ResponseCode 不能为空 !");
		}
			    		this.setValidatorResult("/nsp/NspLoggerUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.nspLoggerService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setNspLogger(NspLogger nspLogger) {
		this.nspLogger = nspLogger;
	}
	
	public NspLogger getNspLogger() {
		return nspLogger;
	}
	 
	 
	 public void setNspLoggerService(NspLoggerService nspLoggerService) {
		this.nspLoggerService = nspLoggerService;
	}
	
	public List<NspLogger> getNspLoggerList() {
		return nspLoggerList;
	}

	public void setNspLoggerList(List<NspLogger> nspLoggerList) {
		this.nspLoggerList = nspLoggerList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_logId() {
        return this.query_logId;
    }
    
    public void setQuery_logId(String logId){
        this.query_logId = logId;
    }    

        public String getQuery_requestTime() {
        return this.query_requestTime;
    }
    
    public void setQuery_requestTime(String requestTime){
        this.query_requestTime = requestTime;
    }    

        public String getQuery_requestInfo() {
        return this.query_requestInfo;
    }
    
    public void setQuery_requestInfo(String requestInfo){
        this.query_requestInfo = requestInfo;
    }    

        public String getQuery_requestType() {
        return this.query_requestType;
    }
    
    public void setQuery_requestType(String requestType){
        this.query_requestType = requestType;
    }    

        public String getQuery_requestIp() {
        return this.query_requestIp;
    }
    
    public void setQuery_requestIp(String requestIp){
        this.query_requestIp = requestIp;
    }    

        public String getQuery_responseData() {
        return this.query_responseData;
    }
    
    public void setQuery_responseData(String responseData){
        this.query_responseData = responseData;
    }    

        public String getQuery_responseInfo() {
        return this.query_responseInfo;
    }
    
    public void setQuery_responseInfo(String responseInfo){
        this.query_responseInfo = responseInfo;
    }    

        public String getQuery_id() {
        return this.query_id;
    }
    
    public void setQuery_id(String id){
        this.query_id = id;
    }    

        public String getQuery_responseCode() {
        return this.query_responseCode;
    }
    
    public void setQuery_responseCode(String responseCode){
        this.query_responseCode = responseCode;
    }

	public Map getRequestTypeMap() {
		return requestTypeMap;
	}

	public void setRequestTypeMap(Map requestTypeMap) {
		this.requestTypeMap = requestTypeMap;
	}

	public Map getResponseCodeMap() {
		return responseCodeMap;
	}

	public void setResponseCodeMap(Map responseCodeMap) {
		this.responseCodeMap = responseCodeMap;
	}

	public String getQuery_requestTime_end() {
		return query_requestTime_end;
	}

	public void setQuery_requestTime_end(String query_requestTime_end) {
		this.query_requestTime_end = query_requestTime_end;
	}

	public String getQuery_requestType_seach() {
		return query_requestType_seach;
	}

	public void setQuery_requestType_seach(String query_requestType_seach) {
		this.query_requestType_seach = query_requestType_seach;
	}    

    
	
}
